package com.geetour.columbus.region.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.geetour.columbus.common.exceptions.ServiceException;
import com.geetour.columbus.common.model.R.CityPage;
import com.geetour.columbus.common.service.ICityPageService;
import com.geetour.columbus.region.bo.ICityPageBO;
import com.geetour.columbus.region.service.ICityPageInnerService;
import com.geetour.columbus.region.utils.CityPageUtil;

@Service
public class CityPageServiceImpl implements ICityPageService, ICityPageInnerService {

    private ICityPageBO cityPageBOImpl;

    @Autowired
    public void setCityPageBOImpl(ICityPageBO cityPageBOImpl) {
        this.cityPageBOImpl = cityPageBOImpl;
    }

    @Override
    public void insertCityPage(CityPage city) throws ServiceException {
        if (city == null) {
            return;
        }
        cityPageBOImpl.insertCityPage(city);
    }

    @Override
    public int insertCityPageNames(String province, List<String> names) throws ServiceException {
        if (province == null || names == null || names.isEmpty()) {
            return -1;
        }
        List<String> insertNames = new ArrayList<String>();
        for (String name : names) {
            CityPage city = cityPageBOImpl.findCityPageByName(name);
            if (city == null) {
                insertNames.add(name);
            }
            insertNames.add(name);
        }
        List<CityPage> cityList = null;
        if (insertNames != null && !insertNames.isEmpty()) {
            cityList = CityPageUtil.getCityByNames(province, names);
            return cityPageBOImpl.insertCityPageBatch(cityList);
        }
        return -1;
    }

    @Override
    public int updateCityPage(CityPage city) throws ServiceException {
        if (city == null) {
            return -1;
        }
        return cityPageBOImpl.updateCityPage(city);
    }

    @Override
    public int deleteCityPageById(Long id) throws ServiceException {
        if (id == null) {
            return -1;
        }
        return cityPageBOImpl.deleteCityPage(id);
    }

    @Override
    public CityPage findCityPageById(Long id) throws ServiceException {
        if (id == null) {
            return null;
        }
        return cityPageBOImpl.findCityPageById(id);
    }

    @Override
    public List<CityPage> findAllCityPages() throws ServiceException {
        return cityPageBOImpl.findAllCityPages();
    }

    @Override
    public List<CityPage> findCityPages(CityPage city) throws ServiceException {
        if (city == null) {
            return null;
        }
        return cityPageBOImpl.findCityPages(city);
    }

    @Override
    public List<CityPage> findCityPagesByProvince(String province) throws ServiceException {
        if (province == null) {
            return null;
        }
        CityPage city = new CityPage();
        city.setProvince(province);
        return cityPageBOImpl.findCityPages(city);
    }

}
